package com.erp.app.report.genericInquiry.inquiryDisplay.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.report.genericInquiry.inquiryDisplay.beans.vo.InquiryDisplayVO;
import com.erp.app.report.genericInquiry.inquiryDisplay.ejb.InquiryDisplayPK;
import com.erp.app.report.genericInquiry.inquiryMaster.ejb.GenericInquiryMasterPK;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;

public class InquiryDisplayDAO extends SimplePageEditStyleDao {
    protected String sqlPurge() {
        return sqlDelete();
    }

    protected void setValuesForPurge(PreparedStatement pStmt, Object obj) throws SQLException {
        setValuesForDelete(pStmt, obj);
    }

    public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
        return super.selectByParent(parent, InquiryDisplayVO.class, conn);
    }

    protected String sqlQuery(SimpleBaseSrchCriteria criteria) {
        //GenericInquirySearchCriteria crit = (GenericInquirySearchCriteria) criteria;

        StringBuffer sb = new StringBuffer();
        sb.append("SELECT COMPANY_CODE, INQUIRY_TYPE, RPT_TIME, LINE_NO, DISP_CODE, DISP_NAME");
        sb.append(", FIELD_NATURE, V_ALIGN, H_ALIGN, HIDDEN_FLAG, COLUMN_WIDTH, CREATE_USER");
        sb.append(", CREATE_DATE, UPDATE_USER, UPDATE_DATE");
        sb.append(", PDF_TBL_COL_WIDTH, PDF_V_ALIGN, PDF_H_ALIGN, PDF_SUMMATION_FLAG, PDF_COL_SPAN, PDF_LBL_H_ALIGN, DISP_URL ");
        sb.append(" FROM T_INQUIRY_DISPLAY ");
        super.logSqlQuery(sb.toString());

        return sb.toString();
    }

//    private String toWhereClause(GenericInquirySearchCriteria criteria) {
//        StringBuffer whereClause = new StringBuffer();
//
//        return whereClause.toString();
//    }

    protected String sqlInsert() {
        return "INSERT INTO T_INQUIRY_DISPLAY (" + "COMPANY_CODE, INQUIRY_TYPE, RPT_TIME, LINE_NO, DISP_CODE, DISP_NAME"
                + ", FIELD_NATURE, V_ALIGN, H_ALIGN, HIDDEN_FLAG, COLUMN_WIDTH, CREATE_USER"
                + ", CREATE_DATE, UPDATE_USER, UPDATE_DATE "
                + ", PDF_TBL_COL_WIDTH, PDF_V_ALIGN, PDF_H_ALIGN, PDF_SUMMATION_FLAG, PDF_COL_SPAN, PDF_LBL_H_ALIGN, DISP_URL)"
                + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    }

    protected void setValuesForInsert(PreparedStatement pStmt, Object obj) throws SQLException {
        InquiryDisplayVO inquiryDisplayVO = (InquiryDisplayVO) obj;
        int i = 1;
        pStmt.setString(i++, inquiryDisplayVO.getCompanyCode());
        pStmt.setString(i++, inquiryDisplayVO.getInquiryType());
        pStmt.setString(i++, inquiryDisplayVO.getRptTime());
        pStmt.setBigDecimal(i++, inquiryDisplayVO.getLineNo());
        setNullableString(pStmt, i++, inquiryDisplayVO.getDispCode());
        setNullableString(pStmt, i++, inquiryDisplayVO.getDispName());
        setNullableString(pStmt, i++, inquiryDisplayVO.getFieldNature());
        setNullableString(pStmt, i++, inquiryDisplayVO.getVertAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getHorizAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getHiddenFlag());
        setNullableBigDecimal(pStmt, i++, inquiryDisplayVO.getColumnWidth());
        setNullableString(pStmt, i++, inquiryDisplayVO.getCreateUser());
        setNullableTimestamp(pStmt, i++, inquiryDisplayVO.getCreateDate());
        setNullableString(pStmt, i++, inquiryDisplayVO.getUpdateUser());
        setNullableTimestamp(pStmt, i++, inquiryDisplayVO.getUpdateDate());
        setNullableBigDecimal(pStmt, i++, inquiryDisplayVO.getPdfTblColWidth());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfVAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfHAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfSummationFlag());
        setNullableBigDecimal(pStmt, i++, inquiryDisplayVO.getPdfColSpan());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfLblHAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getDispURL());

    }

    protected String sqlUpdate() {
        return "UPDATE T_INQUIRY_DISPLAY SET " + "DISP_CODE=?, DISP_NAME=?, FIELD_NATURE=?, V_ALIGN=?, H_ALIGN=?, HIDDEN_FLAG=?"
                + ", COLUMN_WIDTH=?, CREATE_USER=?, CREATE_DATE=?, UPDATE_USER=?, UPDATE_DATE=? "
                + ", PDF_TBL_COL_WIDTH=?, PDF_V_ALIGN=?, PDF_H_ALIGN=?, PDF_SUMMATION_FLAG=?, PDF_COL_SPAN=?, PDF_LBL_H_ALIGN=? "
                + ", DISP_URL=? WHERE COMPANY_CODE = ? " + "AND INQUIRY_TYPE = ? " + "AND RPT_TIME = ? " + "AND LINE_NO = ? "
                + "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";
    }

    protected void setValuesForModify(PreparedStatement pStmt, Object obj) throws SQLException {
        InquiryDisplayVO inquiryDisplayVO = (InquiryDisplayVO) obj;
        int i = 1;
        setNullableString(pStmt, i++, inquiryDisplayVO.getDispCode());
        setNullableString(pStmt, i++, inquiryDisplayVO.getDispName());
        setNullableString(pStmt, i++, inquiryDisplayVO.getFieldNature());
        setNullableString(pStmt, i++, inquiryDisplayVO.getVertAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getHorizAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getHiddenFlag());
        setNullableBigDecimal(pStmt, i++, inquiryDisplayVO.getColumnWidth());
        setNullableString(pStmt, i++, inquiryDisplayVO.getCreateUser());
        setNullableTimestamp(pStmt, i++, inquiryDisplayVO.getCreateDate());
        setNullableString(pStmt, i++, inquiryDisplayVO.getUpdateUser());
        setNullableTimestamp(pStmt, i++, inquiryDisplayVO.getUpdateDate());
        setNullableBigDecimal(pStmt, i++, inquiryDisplayVO.getPdfTblColWidth());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfVAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfHAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfSummationFlag());
        setNullableBigDecimal(pStmt, i++, inquiryDisplayVO.getPdfColSpan());
        setNullableString(pStmt, i++, inquiryDisplayVO.getPdfLblHAlign());
        setNullableString(pStmt, i++, inquiryDisplayVO.getDispURL());
        pStmt.setString(i++, inquiryDisplayVO.getCompanyCode());
        pStmt.setString(i++, inquiryDisplayVO.getInquiryType());
        pStmt.setString(i++, inquiryDisplayVO.getRptTime());
        pStmt.setBigDecimal(i++, inquiryDisplayVO.getLineNo());
        setNullableTimestamp(pStmt, i++, inquiryDisplayVO.getUpdateDate());
    }

    protected String sqlDelete() {
        return "DELETE FROM T_INQUIRY_DISPLAY" + " WHERE COMPANY_CODE = ? " + " AND INQUIRY_TYPE = ? " + " AND RPT_TIME = ? "
                + " AND LINE_NO = ? ";
    }

    protected void setValuesForDelete(PreparedStatement pStmt, Object obj) throws SQLException {
        InquiryDisplayVO inquiryDisplayVO = (InquiryDisplayVO) obj;
        int i = 1;
        pStmt.setString(i++, inquiryDisplayVO.getCompanyCode());
        pStmt.setString(i++, inquiryDisplayVO.getInquiryType());
        pStmt.setString(i++, inquiryDisplayVO.getRptTime());
        pStmt.setBigDecimal(i++, inquiryDisplayVO.getLineNo());
    }

    protected String sqlFind() {
        return "SELECT COMPANY_CODE, INQUIRY_TYPE, RPT_TIME, LINE_NO, DISP_CODE, DISP_NAME"
                + ", FIELD_NATURE, V_ALIGN, H_ALIGN, HIDDEN_FLAG, COLUMN_WIDTH, CREATE_USER"
                + ", CREATE_DATE, UPDATE_USER, UPDATE_DATE "
                + ", PDF_TBL_COL_WIDTH, PDF_V_ALIGN, PDF_H_ALIGN, PDF_SUMMATION_FLAG, PDF_COL_SPAN, PDF_LBL_H_ALIGN, DISP_URL "
                + "FROM T_INQUIRY_DISPLAY " + "WHERE COMPANY_CODE = ? " + "AND INQUIRY_TYPE = ? " + "AND RPT_TIME = ? "
                + "AND LINE_NO = ? ";

    }

    protected void setValuesForFind(PreparedStatement pStmt, Object obj) throws SQLException {
        InquiryDisplayPK pk = (InquiryDisplayPK) obj;
        int i = 1;
        pStmt.setString(i++, pk.getCompanyCode());
        pStmt.setString(i++, pk.getInquiryType());
        pStmt.setString(i++, pk.getRptTime());
        pStmt.setBigDecimal(i++, pk.getLineNo());
    }

    protected String sqlList() {
        return "SELECT COMPANY_CODE, INQUIRY_TYPE, RPT_TIME, LINE_NO, DISP_CODE, DISP_NAME"
                + ", FIELD_NATURE, V_ALIGN, H_ALIGN, HIDDEN_FLAG, COLUMN_WIDTH, CREATE_USER"
                + ", CREATE_DATE, UPDATE_USER, UPDATE_DATE "
                + ", PDF_TBL_COL_WIDTH, PDF_V_ALIGN, PDF_H_ALIGN, PDF_SUMMATION_FLAG, PDF_COL_SPAN, PDF_LBL_H_ALIGN, DISP_URL "
                + "FROM T_INQUIRY_DISPLAY " + "WHERE COMPANY_CODE = ? " + "AND INQUIRY_TYPE = ? "
                + "AND RPT_TIME = ? ORDER BY LINE_NO ASC";
    }

    protected void setValuesForList(PreparedStatement pStmt, Object obj) throws SQLException {
        GenericInquiryMasterPK pk = (GenericInquiryMasterPK) obj;
        int i = 1;
        pStmt.setString(i++, pk.getCompanyCode());
        pStmt.setString(i++, pk.getInquiryType());
        pStmt.setString(i++, pk.getRptTime());
    }

    protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {
        InquiryDisplayVO inquiryDisplayVO = new InquiryDisplayVO();
        inquiryDisplayVO.setCompanyCode(rs.getString("COMPANY_CODE"));
        inquiryDisplayVO.setInquiryType(rs.getString("INQUIRY_TYPE"));
        inquiryDisplayVO.setRptTime(rs.getString("RPT_TIME"));
        inquiryDisplayVO.setLineNo(rs.getBigDecimal("LINE_NO"));
        inquiryDisplayVO.setDispCode(rs.getString("DISP_CODE"));
        inquiryDisplayVO.setDispName(rs.getString("DISP_NAME"));
        inquiryDisplayVO.setFieldNature(rs.getString("FIELD_NATURE"));
        inquiryDisplayVO.setVertAlign(rs.getString("V_ALIGN"));
        inquiryDisplayVO.setHorizAlign(rs.getString("H_ALIGN"));
        inquiryDisplayVO.setHiddenFlag(rs.getString("HIDDEN_FLAG"));
        inquiryDisplayVO.setColumnWidth(rs.getBigDecimal("COLUMN_WIDTH"));
        inquiryDisplayVO.setCreateUser(rs.getString("CREATE_USER"));
        inquiryDisplayVO.setCreateDate(rs.getTimestamp("CREATE_DATE"));
        inquiryDisplayVO.setUpdateUser(rs.getString("UPDATE_USER"));
        inquiryDisplayVO.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
        inquiryDisplayVO.setPdfTblColWidth(rs.getBigDecimal("PDF_TBL_COL_WIDTH"));
        inquiryDisplayVO.setPdfVAlign(rs.getString("PDF_V_ALIGN"));
        inquiryDisplayVO.setPdfHAlign(rs.getString("PDF_H_ALIGN"));
        inquiryDisplayVO.setPdfSummationFlag(rs.getString("PDF_SUMMATION_FLAG"));
        inquiryDisplayVO.setPdfColSpan(rs.getBigDecimal("PDF_COL_SPAN"));
        inquiryDisplayVO.setPdfLblHAlign(rs.getString("PDF_LBL_H_ALIGN"));
        inquiryDisplayVO.setDispURL(rs.getString("DISP_URL"));
        return inquiryDisplayVO;
    }
}
